package com.xbts.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.CnHomeProductType;
import com.xbts.system.service.ICnHomeProductTypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.mapper.CnHomeProductMapper;
import com.xbts.system.domain.CnHomeProduct;
import com.xbts.system.service.ICnHomeProductService;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;

/**
 * 首页产品信息Service业务层处理
 * 
 * @author xbts
 * @date 2023-11-30
 */
@Service
public class CnHomeProductServiceImpl  extends ServiceImpl<CnHomeProductMapper, CnHomeProduct> implements ICnHomeProductService {


    @Autowired
    private ICnHomeProductTypeService cnHomeProductTypeService;

    /**
     * 查询首页产品信息列表
     *
     * @param cnHomeProduct 首页产品信息
     * @return 首页产品信息
     */
    @Override
    public List<CnHomeProduct> selectCnHomeProductList(CnHomeProduct cnHomeProduct) {
        QueryWrapper<CnHomeProduct> queryWrapper = cnHomeProduct.queryWrapper();
        if(!SecurityUtils.isManager()) {
            queryWrapper.eq("status", 1);
        }
        List<CnHomeProduct> list = list(queryWrapper);
        List<CnHomeProductType> types = cnHomeProductTypeService.list();
        Map<String, CnHomeProductType> typeMap = types.stream().collect(Collectors.toMap(CnHomeProductType::getId, v -> v));

        for (CnHomeProduct product : list) {
            if (typeMap.containsKey(product.getTypeId())) {
                product.setTypeName(typeMap.get(product.getTypeId()).getTypeName());
            } else {
                product.setTypeName("未知");
            }
            if (typeMap.containsKey(product.getChildrenTypeId())) {
                product.setChildrenTypeName(typeMap.get(product.getChildrenTypeId()).getTypeName());
            } else {
                product.setChildrenTypeName("未知");
            }
        }
        return list;
    }

    /**
     * 查询首页产品信息
     *
     * @param id 首页产品信息主键
     * @return 首页产品信息
     */
    @Override
    public CnHomeProduct selectCnHomeProductById(String id) {
        CnHomeProduct homeProduct = getById(id);
        CnHomeProductType productType = cnHomeProductTypeService.getById(homeProduct.getTypeId());
        if (productType!= null) {
            homeProduct.setTypeName(productType.getTypeName());
        } else {
            homeProduct.setTypeName("未知");
        }
        return homeProduct;
    }

    /**
     * 新增首页产品信息
     *
     * @param cnHomeProduct 首页产品信息
     * @return 结果
     */
    @Override
    public boolean insertCnHomeProduct(CnHomeProduct cnHomeProduct) {
        CnHomeProductType type = cnHomeProductTypeService.getById(cnHomeProduct.getTypeId());
        if(type.getHaveChildren().equals("1")){
            String childrenTypeId = cnHomeProduct.getChildrenTypeId();
            if(StringUtils.isBlank(childrenTypeId)){
                throw new ServiceException("该类型存在子类型，请选择子类型");
            }
        }

        cnHomeProduct.setCreateBy(SecurityUtils.getUserId().toString());
        return save(cnHomeProduct);
    }

    /**
     * 修改首页产品信息
     *
     * @param cnHomeProduct 首页产品信息
     * @return 结果
     */
    @Override
    public boolean updateCnHomeProduct(CnHomeProduct cnHomeProduct) {
        cnHomeProduct.setUpdateBy(SecurityUtils.getUserId().toString());
        cnHomeProduct.setUpdateTime(DateUtils.getNowDate());
        CnHomeProductType type = cnHomeProductTypeService.getById(cnHomeProduct.getTypeId());
        if(type.getHaveChildren().equals("1")){
            String childrenTypeId = cnHomeProduct.getChildrenTypeId();
            if(StringUtils.isBlank(childrenTypeId)){
                throw new ServiceException("该类型存在子类型，请选择子类型");
            }
        }
        return updateById(cnHomeProduct);
    }

    /**
     * 批量删除首页产品信息
     *
     * @param ids 需要删除的首页产品信息主键
     * @return 结果
     */
    @Override
    public boolean deleteCnHomeProductByIds(List<String> ids) {
        return removeBatchByIds(ids);
    }

    /**
     * 删除首页产品信息信息
     *
     * @param id 首页产品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnHomeProductById(String id) {
      return removeById(id);
    }

    @Override
    public List<CnHomeProductType> allList() {
        CnHomeProductType type = new CnHomeProductType();
        type.setStatus("1");
        List<CnHomeProductType> types = cnHomeProductTypeService.list(type.queryWrapper());

        CnHomeProduct product = new CnHomeProduct();
        product.setStatus("1");
        List<CnHomeProduct> products = this.list(product.queryWrapper());
        for (CnHomeProductType productType : types) {
            // 获取该类型下所有上架的产品 并排序
            for (CnHomeProduct cnHomeProduct : products) {
                if(cnHomeProduct.getTypeId().equals(productType.getId()))
                productType.getProductList().add(cnHomeProduct);
            }
        }

        return types;
    }

    @Override
    public Object listByUser(String typeId) {
        CnHomeProductType byId = cnHomeProductTypeService.getById(typeId);

        // 该类型存在子类型
        if(byId.getHaveChildren().equals("1")){
            CnHomeProductType type = new CnHomeProductType();
            type.setParentId(typeId);
            // 查询所有子类型
            List<CnHomeProductType> types = cnHomeProductTypeService.list(type.queryWrapper());
            // 查询所有该类型的产品
            CnHomeProduct product = new CnHomeProduct();
            product.setTypeId(typeId);
            product.setStatus("1");
            QueryWrapper<CnHomeProduct> queryWrapper = product.queryWrapper();
            List<CnHomeProduct> products = this.list(queryWrapper);
            // 封装数据
            for (CnHomeProductType productType : types) {
                for (CnHomeProduct cnHomeProduct : products) {
                    if(cnHomeProduct.getChildrenTypeId().equals(productType.getId()))
                    productType.getProductList().add(cnHomeProduct);
                }
            }
            return types;
        }
        // 不存在子类型则直接返回该类型下的所有产品
        return this.list(new LambdaQueryWrapper<CnHomeProduct>()
                .eq(CnHomeProduct::getTypeId, typeId)
                .eq(CnHomeProduct::getStatus, 1)
                .orderByAsc(CnHomeProduct::getSort));
    }

}
